package base.synchronizedstudy;

/**
 * 有两个线程，处理同一个英雄。
一个加血，一个减血。
减血的线程，发现血量=1，就停止减血，直到加血的线程为英雄加了血，才可以继续减血     1.普通方法，使用while
 * @author admin
 *
 */
		
public class Wait_Notify {

	public static void main(String[] args) {
		final Hero gareen = new Hero();
		gareen.name="盖伦";
		gareen.hp=666;
		Thread t1 = new Thread(){
			public void run(){
				while(true){
					while(gareen.hp==1){//跳出的只是这一个while循环
						continue;
					}
					gareen.hurt();
					System.out.printf("t1 为%s减血1点，减少血后，%s的血量是%.0f%n",gareen.name,gareen.name,gareen.hp);
						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		};
		t1.start();
		Thread t2 = new Thread(){
			public void run(){
				while(true){
					gareen.recover();
					System.out.printf("t2 为%s 回血1点,增加血后，%s的血量是%.0f%n",gareen.name,gareen.name,gareen.hp);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		t2.start();
	}
}
